Quantitative Analysis in Political Science
ggplotRecall (again…)
ggplot() is the plot function
(Data =) is where you specify the dataset that you are usinggeom_point() is an added layer, which specifies how the data will be plottedmapping defines how variables in your dataset are mapped to visual propertiesaes specify which variables to map to the x and y axesfacet_wrap(~ <VARIABLE NAME>)ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
facet_wrap(~ class, nrow = 2)ggplot takes a mapping argument
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))ggplot(), this will treat the mapping as global and apply to EACH geomgeom, this will treat the mapping as local and apply to ONLY that geomggplot functionscoord_flip flips the x and y axis to improve the readability of plotsscales change the formatting of x and y axesplotly makes plots interactive; you can hover over points/lines for more informationlabs allows you to add/edit a title, subtitle, a caption, and change the x and y axis labelsgganimate allows you to animate plots into gifs# A tibble: 53,940 × 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
# ℹ 53,930 more rows
stat_count()ggplot() compute this?geom_bar() utilizes stat_count() as the default way to make statistical transformations for bar graphsgeoms and stats interchangeably.Most of the time, we are not going to be concerned with changing stat
stat = "identity"
demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)
ggplot(data = demo) +
geom_bar(mapping = aes(x = cut, y = freq), stat = "identity")y = stat(prop), group = 1color =fill =position = is left unchanged#adding a title
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
labs(title = "Cut of Diamond by Clarity")#adding a x-axis
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
labs(title = "Cut of Diamond by Clarity",
x = "Diamond Cut")#adding a y-axis
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
labs(title = "Cut of Diamond by Clarity",
x = "Diamond Cut",
y = "Number of Diamonds")ggplot() automatically adds default scales behind the scenes#change y-axis scale
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
labs(title = "Cut of Diamond by Clarity",
x = "Diamond Cut",
y = "Number of Diamonds") +
scale_y_continuous(breaks = seq(0, 6000, by = 500), limits = c(0, 6000)) #change x-axis scale (labels)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
labs(title = "Cut of Diamond by Clarity",
x = "Diamond Cut",
y = "Number of Diamonds") +
scale_y_continuous(breaks = seq(0, 6000, by = 500), limits = c(0, 6000)) +
scale_x_discrete(labels = c("eh", "ok", "better", "wow", "hey now"))#default theme
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")#theme_bw()
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
theme_bw()#theme_bw() with grid removed
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
scale_y_continuous(breaks = seq(0, 6000, by = 500), limits = c(0, 6000)) +
theme_bw() +
theme(axis.line = element_line(color='black'),
plot.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank())#theme_bw() with origin at 0,0
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +
scale_y_continuous(breaks = seq(0, 6000, by = 500), limits = c(0, 6000), expand = c(0,0)) +
theme_bw() +
theme(axis.line = element_line(color='black'),
plot.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank())We are going to practice producing and altering ggplot()
Get with a partner and work through Week 3 | Class Activity
Review what you learned today
Do new set of readings:
Bring your laptops to class